package bvrtc

import (
	psdp "github.com/pion/sdp/v3"
	"gogs.smarteye.com/smarteye/gortc/pkg/description"
)

type Media struct {
	pc    *PeerConnection
	local bool

	media   *description.Media // video/audio
	formats map[uint8]*Format  // H264/H265/AAC/OPUS
}

func newMedia(pc *PeerConnection, m *description.Media, local bool) *Media {
	cm := &Media{
		pc:      pc,
		local:   local,
		media:   m,
		formats: map[uint8]*Format{},
	}

	for _, f := range m.Formats {
		cm.formats[f.PayloadType()] = newFormat(cm, f)
	}

	return cm
}

func (cm *Media) close() {
	for _, f := range cm.formats {
		f.close()
	}
}

func (cm *Media) isLocal() bool {
	return cm.local
}

func (cm *Media) HaveSender() bool {
	if cm.media.Direction == psdp.DirectionSendOnly || cm.media.Direction == psdp.DirectionSendRecv {
		return true
	}

	return false
}

func (cm *Media) HaveReceiver() bool {
	if cm.media.Direction == psdp.DirectionRecvOnly || cm.media.Direction == psdp.DirectionSendRecv {
		return true
	}

	return false
}
